﻿Imports HMSDataLayer
Imports HMSLibrary
Imports System.Collections.ObjectModel

Public Class PatientSearchControl

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Dim lstCl As List(Of Lookups)
    Public Overloads Sub LoadData(Optional isDischargeList As Boolean = False)
        Dim dataProvider As New PatientDataLayer()
        If isDischargeList = True Then
            lstCl = dataProvider.GetIPDPatientNameIDList()
        Else
            lstCl = dataProvider.GetPatientNameIDList()
        End If

        IdTextBox.autoCompletionList = New ObservableCollection(Of AutoCompleteEntry)()
        NameTextBox.autoCompletionList = New ObservableCollection(Of AutoCompleteEntry)()
        For Each item In lstCl
            IdTextBox.AddItem(New AutoCompleteEntry(item.ID, item.ID, item.Name, item.ID))
            NameTextBox.AddItem(New AutoCompleteEntry(item.Name, item.ID, item.Name, item.Name))
        Next
    End Sub

    Private Sub IdTextBox_ValueChanged(sender As Object) Handles IdTextBox.ValueChanged
        _SelectedItem = DirectCast(sender, AutoCompleteEntry)
        NameTextBox.Text = SelectedItem.Value
        RaiseEvent SelectedValueChanged(sender)
    End Sub

    Private Sub NameTextBox_ValueChanged(sender As Object) Handles NameTextBox.ValueChanged
        _SelectedItem = DirectCast(sender, AutoCompleteEntry)
        IdTextBox.Text = SelectedItem.Id
        RaiseEvent SelectedValueChanged(sender)
    End Sub

    Dim _SelectedItem As AutoCompleteEntry
    Public ReadOnly Property SelectedItem As AutoCompleteEntry
        Get
            Return _SelectedItem
        End Get
    End Property

    Public Overloads Sub LoadData(id As Integer)
        If id = 0 Then
            NameTextBox.Text = String.Empty
            IdTextBox.Text = String.Empty
        End If
        Dim patient = (From p In lstCl Where p.ID = id).FirstOrDefault
        If patient IsNot Nothing Then
            NameTextBox.Text = patient.Name
            IdTextBox.Text = patient.ID
            _SelectedItem = New AutoCompleteEntry(patient.Name, patient.ID, patient.Name, patient.Name)
        End If
    End Sub

    Public Event SelectedValueChanged(sender As Object)

    Private Sub PatientTextBlock_MouseLeftButtonDown(sender As System.Object, e As System.Windows.Input.MouseButtonEventArgs)
        Dim w As New PatientRegistrationWindow
        w.ShowDialog()
        LoadData()
    End Sub
End Class
